/**
 * 角色
 * @param {Object} main
 */
Ext.extend(Bee.module, {
    init: function(main){
        var setValue = function(o, l){
            if (o.root.childNodes.length == 0) {
                setValue.defer(100, o, [o, l]);
            }
            else {
                o.setValue(l);
            }
        };
        
        var grid = new Bee.grid.PageGrid({
            id: 'sm-roleMan-grid',
            region: 'center',
            initload: true,
            firstColumn: 'radio',
            filter: {
                eq_status: 1,
                asc_sort: 1
            },
            dwr: {
                call: smDwr.pageRole
            },
            metas: [{
                id: 'sid',
                field: 'sid',
                type: 'int',
                hidden: true
            }, {
                field: 'ename',
                desc: '编码'
            }, {
                field: 'cname',
                desc: '名称'
            }, {
                field: 'comments',
                desc: '备注'
            }, {
                field: 'sort',
                desc: '排序'
            }],
            listeners: {
                render: function(o){
                    o.getSelectionModel().on('selectionchange', function(p){
                        if (p.getCount() === 0) {
                            tabPanel.setActiveTab(0);
                            infoForm.getForm().reset();
                            Ext.getCmp('sm-roleMan-infoForm-saveBtn').enable();
                            Ext.getCmp('sm-roleMan-infoForm-updateBtn').disable();
                            Ext.getCmp('sm-roleMan-infoForm-resetBtn').enable();
                            Ext.getCmp('sm-roleMan-infoForm-deleteBtn').disable();
                            formPanel.checkedNodes = [];
                            if (formPanel.rendered) {
                                formPanel.root.reload();
                            }
                        }
                        if (p.getCount() === 1) {
                            infoForm.load(o.getSelectedRow().sid);
                            Ext.getCmp('sm-roleMan-infoForm-saveBtn').disable();
                            Ext.getCmp('sm-roleMan-infoForm-updateBtn').enable();
                            Ext.getCmp('sm-roleMan-infoForm-resetBtn').enable();
                            Ext.getCmp('sm-roleMan-infoForm-deleteBtn').enable();
                            smDwr.findResourceByRole(o.getSelectedRow().sid, function(out){
                                var idList = [];
                                for (var i = 0; i < out.length; i++) {
                                    idList.push(out[i].sid);
                                }
                                formPanel.checkedNodes = idList;
                                setValue(formPanel, idList);
                            });
                            if (formPanel.rendered) {
                                formPanel.root.reload();
                            }
                        }
                    });
                }
            }
        });
        
        var infoForm = new Bee.form.DWRForm({
            id: 'sm-roleMan-infoForm',
            title: '详情',
            width: 400,
            labelWidth: 85,
            action: 'save',
            dwr: {
                save: smDwr.saveRole,
                update: smDwr.updateRole,
                load: smDwr.getRole
            },
            afterSubmit: function(){
                grid.reload();
            },
            items: [{
                fieldLabel: 'sid',
                name: 'sid',
                xtype: 'hidden'
            }, {
                fieldLabel: 'status',
                name: 'status',
                xtype: 'hidden',
                value: 1
            }, {
                fieldLabel: 'smOrganizationSid',
                name: 'smOrganizationSid',
                xtype: 'hidden'
            }, {
                fieldLabel: '编码',
                name: 'ename',
                allowBlank: false,
                maxLength: 100
            }, {
                fieldLabel: '名称',
                name: 'cname',
                allowBlank: false,
                maxLength: 100
            }, {
                fieldLabel: '备注',
                name: 'comments',
                xtype: 'textarea',
                allowBlank: true,
                maxLength: 100
            }, {
                fieldLabel: '排序',
                name: 'sort',
                xtype: 'numberfield',
                allowDecimals: false,
                allowNegative: false,
                allowBlank: false,
                maxLength: 3
            }, {
                layout: 'hbox',
                xtype: 'panel',
                bodyCssClass: 'x-bee-form-hbox',
                layoutConfig: {
                    padding: '5',
                    pack: 'center',
                    align: 'middle'
                },
                defaults: {
                    xtype: 'button',
                    minWidth: 75,
                    hidden: false,
                    margins: '0 10 0 0'
                },
                items: [{
                    id: 'sm-roleMan-infoForm-saveBtn',
                    text: '新增',
                    iconCls: 'btn_insert',
                    handler: function(){
                        Ext.Msg.confirm('提示', '确认保存吗?', function(param){
                            if (param == 'yes') {
                                infoForm['save']();
                            }
                        });
                    }
                }, {
                    id: 'sm-roleMan-infoForm-updateBtn',
                    text: '修改',
                    iconCls: 'btn_modify',
                    disabled: true,
                    handler: function(){
                        Ext.Msg.confirm('提示', '确认保存吗?', function(param){
                            if (param == 'yes') {
                                infoForm['update']();
                            }
                        });
                    }
                }, {
                    id: 'sm-roleMan-infoForm-resetBtn',
                    text: '刷新',
                    iconCls: 'btn_refresh',
                    handler: function(){
                        if (!grid.getSelectedRow()) {
                            infoForm.getForm().reset();
                        }
                        else {
                            infoForm.load(grid.getSelectedRow().sid);
                        }
                    }
                }, {
                    id: 'sm-roleMan-infoForm-deleteBtn',
                    text: '删除',
                    iconCls: 'btn_delete',
                    disabled: true,
                    handler: function(){
                        Ext.Msg.confirm('提示', '确认删除吗?', function(param){
                            if (param == 'yes') {
                                smDwr.deleteRole(grid.getSelectedRow().sid, function(){
                                    Bee.msg('删除成功!');
                                    grid.reload();
                                });
                            }
                        });
                    }
                }]
            }]
        });
        
        var formPanel = new Ext.ux.tree.CheckTreePanel({
            id: 'sm-roleMan-formPanel',
            title: '关联资源',
            bubbleCheck: 'unchecked',
            useArrows: true,
            stateful: false,
            autoScroll: true,
            border: false,
            rootVisible: false,
            loader: new Bee.tree.DWRTreeLoader({
                dwrCall: smDwr.findMenuExp,
                nodeMeta: {
                    id: 'sid',
                    text: 'cname',
                    leaf: 'isLeaf',
                    checked: 'checked'
                }
            }),
            root: {
                nodeType: 'async',
                text: '资源',
                draggable: false,
                sid: '0',
                cname: '资源',
                leaf: false,
                expanded: true
            },
            tbar: [{
                id: 'sm-roleMan-formPanel-saveBtn',
                text: '保存',
                iconCls: 'btn_modify',
                handler: function(){
                    Ext.Msg.confirm('提示', '确认保存吗?', function(param){
                        if (param == 'yes') {
                            smDwr.saveRoleAndResource(grid.getSelectedRow().sid, formPanel.getValue(), function(){
                                tabPanel.setActiveTab(0);
                                Bee.msg('保存成功！');
                            });
                        }
                    });
                }
            }]
        });
        
        var tabPanel = new Ext.TabPanel({
            id: 'sm-roleMan-tabPanel',
            region: 'east',
            cls: 'x-bee-form-left',
            border: false,
            width: 400,
            activeItem: 0,
            items: [infoForm, formPanel],
            listeners: {
                beforetabchange: function(o, newTab, curTab){
                    if (newTab == formPanel) {
                        if (!grid.getSelectedRow()) {
                            Bee.msg('请选择要关联的数据!');
                            return false;
                        }
                    }
                }
            }
        });
        
        var panel = new Ext.Panel({
            id: 'sm-roleMan-panel',
            layout: 'border',
            border: false,
            items: [grid, tabPanel],
            loadFlag: true
        });
        
        this.main.associatedComp = [];
        this.main.add(panel);
        this.main.doLayout();
    }
});
